package com.soboot.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soboot.base.converter.BeanMapping;
import com.soboot.system.entity.SysUser;
import com.soboot.system.exception.ResCode;
import com.soboot.system.exception.SystemException;
import com.soboot.system.mapper.SysUserMapper;
import com.soboot.system.service.ISysRoleMenuService;
import com.soboot.system.service.ISysUserRoleService;
import com.soboot.system.service.IUserService;
import com.soboot.system.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:
 * @createTime: 2022年09月21日 13:23:43
 * @version:
 * @Description: 用户service实现类
 * @Copyright:
 */
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements IUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public UserInfoVO queryUser(String userName) {

        SysUser sysUser = sysUserMapper.selectOne(Wrappers.<SysUser>query()
                .lambda().eq(SysUser::getUserName, userName));
        if(null == sysUser){
            throw new SystemException(ResCode.FAIL,"账户不存在");
        }
        List<Integer> roleIdList = sysUserRoleService.selectRoleByUserId(sysUser.getId());
        List<String> menuList = sysRoleMenuService.selectMenuByRoleId(roleIdList);

        UserInfoVO vo = BeanMapping.map(sysUser,UserInfoVO.class);
        vo.setPermissions(menuList);
        return vo;
    }
}
